﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using com.YiXiong.XML;
using System.Windows.Forms;

namespace QYWebSiteBN
{
    public partial class Form1 : Form
    {
        private static readonly string websiteconfig = ConfigurationSettings.AppSettings["WebSiteFile"];
        private static readonly string dbfile = ConfigurationSettings.AppSettings["DBFile"];

        public Form1()
        {

            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            updateTask();
            updateDBList();
        }

        public void updateTask()
        {
            string dir = System.IO.Directory.GetCurrentDirectory();

            string file = string.Format("{0}/{1}", System.IO.Directory.GetCurrentDirectory(), websiteconfig);

            XMLHelper xml = new XMLHelper(file);

            XmlAttributeCollection ac = xml.GetChildNodesAttributesByAttributeName("WebSites", "Name");

            tvTask.Nodes.Clear();

            foreach(XmlAttribute a in  ac)
            {
                TreeNode node = new TreeNode(a.Value);
                string key=string.Format("WebSites/WebSite[@Name='{0}']",a.Value);
                node.Name = key;


                XmlAttributeCollection acs = xml.GetChildNodesAttributesByAttributeName(key, "Name");

                foreach (XmlAttribute at in acs)
                {
                     
                    node.Nodes.Add(string.Format("{0}/Task[@Name='{1}']",key,at.Value),at.Value);
                }
                tvTask.Nodes.Add(node);
            }            

        }

        public void updateDBList()
        {
            string dir = System.IO.Directory.GetCurrentDirectory();

            string file = string.Format("{0}/{1}", System.IO.Directory.GetCurrentDirectory(), dbfile);

            XMLHelper xml = new XMLHelper(file);

            XmlAttributeCollection databases_ac = xml.GetChildNodesAttributesByAttributeName("Databases", "Name");//获得Databases下面的子节点,如:MSSQL/MYSQL/ACCRECC


          //  XmlAttributeCollection databases_ac = xml.GetChildNodesAttributesByAttributeName("Databases");//获得Databases下面的子节点,如:MSSQL/MYSQL/ACCRECC

            tvDBList.Nodes.Clear();

            

            foreach (XmlAttribute databases_a in databases_ac)//获得Databases下面的子节点,如:MSSQL/MYSQL/ACCRECC
            {
                TreeNode node = new TreeNode(databases_a.Value);


                String key = string.Format("Databases/DataBaseType[@Name='{0}']", databases_a.Value);
                node.Name = key;


                XmlAttributeCollection server_ac = xml.GetChildNodesAttributesByAttributeName(key);//获得如MYSQL下面的子节点Server

                foreach (XmlAttribute server_a in server_ac)
                {
                    
                    key = string.Format("{0}/{1}", key,server_a.Name);//获得如MYSQL下面的子节点

                    XmlAttributeCollection server_ac_list=  xml.GetNodeAttributes(key);

                    TreeNode snode = new TreeNode(server_ac_list[0].Value);//数据库服务器

                    key = string.Format("{0}[@Name='{1}']", key, server_ac_list[0].Value);//获得指定的数据库库服务器

                     XmlAttributeCollection database_ac = xml.GetChildNodesAttributesByAttributeName(key, "Name");//对数据库获取

                     foreach (XmlAttribute at in database_ac)
                     {
                         snode.Nodes.Add(at.Name, at.Value);
                     }
                    
                    //node.Nodes.Add(string.Format("{0}/[@Name='{1}']", key, server_ac_list[0].Value), );
                     node.Nodes.Add(snode);
                }
                tvDBList.Nodes.Add(node);
            }            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form_EditTack fedit = new Form_EditTack();
            fedit.Show();

        }

        private void MItemRuleList_Click(object sender, EventArgs e)
        {
            Form_ContentRuleList ruleList = new Form_ContentRuleList();
            ruleList.Owner = this;
            ruleList.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form_WebSite website = new Form_WebSite();
            website.ShowDialog();
        }

        private void splitContainer1_Resize(object sender, EventArgs e)
        {
        

                   
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            splitContainer1.SplitterDistance = 288;
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            
        }

        private void tvTask_DoubleClick(object sender, EventArgs e)
        {
            Form_EditTack task = new Form_EditTack(tvTask.SelectedNode.Name);
            task.Owner = this;
            task.ShowDialog();
           // MessageBox.Show(tvTask.SelectedNode.Name);
        }


        private void NewWebSiteMenuItem_Click(object sender, EventArgs e)
        {

            Form_WebSite website = new Form_WebSite();
            website.ShowDialog();
        }

        private void NewTaskMenuItem_Click(object sender, EventArgs e)
        {
            Form_EditTack tesk = new Form_EditTack();
            tesk.Owner = this;
            tesk.ShowDialog();
        }

        private void menuAddDb_Click(object sender, EventArgs e)
        {
            fmDBSelect db = new fmDBSelect();
            db.ShowDialog();
        }

        private void menuDB_ad_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.ShowDialog();
        }


       
    }
}
